﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using System.Net;
using System.Xml;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Data;
using ThreeByte.ActiveDeck.Service;
using System.Threading;
using ThreeByte.ActiveDeck.Content;
using Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.Media;
using ThreeByte.ActiveDeck.AddIn.Controls;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;


namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for ActionSetupControl.xaml
    /// </summary>
    public partial class ActionSetupControl : UserControl, ICloseable
    {

        public event EventHandler<DialogResultEventArgs> Close;

        public CoreConfig Config;

        public ActionSetupControl(CoreConfig config, NavigationAction currentAction = null) {
            InitializeComponent();
            Config = config;
            Tag = "Configure Action"; //Implemented to support customized title bar in Forms Host

            //Read and initialize current value, if applicable
            if(currentAction != null) {
                InitWithAction(currentAction);
            }

            ActionTypeComboBox.SelectionChanged += new SelectionChangedEventHandler(ActionTypeComboBox_SelectionChanged);
        }

        private void InitWithAction(NavigationAction action) {

            if(action.Command) {
                ActionTypeComboBox.SelectedIndex = 5;
                DataCommandControl commandControl = new DataCommandControl(action.CommandData);
                commandControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                ContentRoot.Content = commandControl;
            }

        }

        public NavigationAction ConfiguredAction {
            get;
            private set;
        }


        private void ActionTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            int selectedIndex = ActionTypeComboBox.SelectedIndex;

            if(ContentRoot.Content != null &&
                ContentRoot.Content is ICloseable) {
                    ICloseable closeableControl = (ICloseable)(ContentRoot.Content);
                    closeableControl.Close -= subControl_Close;
            }

            switch (selectedIndex) {
                case 0: {
                        ConfiguredAction = new NavigationAction() { Next = true };
                        //Show a done/cancel control
                        EmptyChooserControl emptyControl = new EmptyChooserControl();
                        emptyControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                        ContentRoot.Content = emptyControl;
                        break;
                    }
                case 1: {
                        ConfiguredAction = new NavigationAction() { Back = true };
                        //Show a done/cancel control
                        EmptyChooserControl emptyControl = new EmptyChooserControl();
                        emptyControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                        ContentRoot.Content = emptyControl;
                        break;
                    }
                case 2: {
                        ConfiguredAction = new NavigationAction() { Submit = true };
                        EmptyChooserControl emptyControl = new EmptyChooserControl();
                        emptyControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                        ContentRoot.Content = emptyControl;
                        break;
                    }
                case 3: {
                        ConfiguredAction = new NavigationAction() { Save = true, Submit = false };
                        EmptyChooserControl emptyControl = new EmptyChooserControl();
                        emptyControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                        ContentRoot.Content = emptyControl;
                        break;
                    }
                case 4: {
                        PageChooserControl pageControl = new PageChooserControl(Config);
                        pageControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                        ContentRoot.Content = pageControl;
                        break;
                    }
                case 5: {
                        LayerChooserControl layerControl = new LayerChooserControl();
                        layerControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                        ContentRoot.Content = layerControl;
                        break;
                    }
                case 6: { //For publishing data commands
                        DataCommandControl commandControl = new DataCommandControl();
                        commandControl.Close += new EventHandler<DialogResultEventArgs>(subControl_Close);
                        ContentRoot.Content = commandControl;
                        break;
                    }
                default:
                    break;
            }

        }

        void subControl_Close(object sender, DialogResultEventArgs e) {
            if(ContentRoot.Content != null) {
                if(ContentRoot.Content is LayerChooserControl) {
                    LayerChooserControl layerChooser = (LayerChooserControl)(ContentRoot.Content);
                    ConfiguredAction = layerChooser.ConfiguredAction;

                }
                if(ContentRoot.Content is PageChooserControl) {
                    PageChooserControl pageChooser = (PageChooserControl)(ContentRoot.Content);
                    PageConfig selectedPage = pageChooser.SelectedPage;
                    ConfiguredAction = new NavigationAction() { Section = selectedPage.ParentSlideConfig.SlideId, Page = selectedPage.PageId, Jump = true };
                    if(selectedPage is NavigatorConfig) {
                        ConfiguredAction.JumpOffset = true;
                        ConfiguredAction.HOffset = pageChooser.HOffset;
                        ConfiguredAction.VOffset = pageChooser.VOffset;
                    }
                }
                if(ContentRoot.Content is DataCommandControl) {
                    DataCommandControl commandControl = (DataCommandControl)(ContentRoot.Content);
                    ConfiguredAction = commandControl.ConfiguredAction;
                }
            }
            if(Close != null) {
                Close(this, e);
            }
        }
  
    }
}
